var mysql = require('mysql')
var async = require('async');
var gameConfig = require("./../config/gameConfig");
var mysql_config=require("./../../util/mysql_config");
var log = require("./../../util/loginfo").getInstand;

var pool = mysql.createPool({
    connectionLimit: 10000,
    host:mysql_config.host,
    user:mysql_config.user,
    password:mysql_config.password,
    port:mysql_config.port,
    database: 'la_ba'
})


//获得免费次数
exports.getFreeCount = function getFreeCount(userInfo, callback_c) {

    pool.getConnection(function (err, connection) {

        var values = [];
        values.push(parseInt(gameConfig.gameId.toString() + userInfo.toString()));

        async.waterfall([
            function (callback) {
                var sql = "select * from useraccounts where Id=?";
                connection.query({sql: sql, values: values}, function (err, rows) {
                    //values = [];
                    if (err) {
                        log.err(err);
                        callback(err);
                        //callback(0);
                    } else {
                        callback(null, rows);
                    }
                })

            },
            function (arg1, callback) {
                if (!arg1.length) {
                    var sql = "INSERT INTO useraccounts(Id) VALUES(?)";
                    connection.query({sql: sql, values: values}, function (err, rows) {
                        values = [];
                        if (err) {
                            log.err(err);
                            callback(err);
                        } else {
                            var Result = {Id: userInfo._userId, freeCount: 0, LotteryCount: 0}
                            //log.info("插入");
                            callback(null, Result);
                        }
                    })
                } else {
                    callback(null, arg1[0]);
                }

            }
        ], function (err, result) {
            // result now equals 'done'
            //log.info(result)
            if (err) {
                log.err(err);
                log.info(result);
                callback_c(0);
            } else {
                callback_c(1, result);
            }
            //log.info("1end");

            connection.release();
            values = [];
        });
    });
}

//获得免费次数
exports.saveFree = function saveFree(userInfo, callback) {

    var sql = "UPDATE useraccounts SET freeCount=?,LotteryCount=? WHERE Id =?";
    var values = [];

    values.push(userInfo.freeCount);
    values.push(userInfo.LotteryCount);
    values.push(parseInt(gameConfig.gameId.toString() + userInfo.userId.toString()));
    pool.getConnection(function (err, connection) {

        connection.query({sql: sql, values: values}, function (err, rows) {
            if (err) {
                log.err(err);
                callback(0);
            } else {
                callback(1);
            }
        })

        connection.release();
        values = [];

    });
}


//金币记录
exports.score_changeLog = function score_changeLog(userInfo) {
    var sql = "INSERT INTO gameaccount.score_changelog(userid,score_before,score_change,score_current,change_type,isOnline) VALUES(?,?,?,?,?,?)";
    var values = [];

    pool.getConnection(function (err, connection) {
        for (var i = 0; i < userInfo.length; i++) {
            values.push(userInfo[i].userid);
            values.push(userInfo[i].score_before);
            values.push(userInfo[i].score_change);
            values.push(userInfo[i].score_current);
            values.push(userInfo[i].change_type);
            values.push(userInfo[i].isOnline);

            connection.query({sql: sql, values: values}, function (err, rows) {
                if (err) {
                    log.info("score_changeLog");
                    log.err(err);
                }
            })
            values = [];
        }


        connection.release();
        values = [];

    });
}


//奖池记录
exports.Update_score_pool = function Update_score_pool(poollist, Virtualpool, poollistId, callback) {
    var sql = "UPDATE score_pool SET score_pool=? WHERE id = ?";

    pool.getConnection(function (err, connection) {
        for (var i = 0; i <= poollistId.length; ++i) {
            var values = [];
            if (i != poollistId.length) {
                values.push(poollist[i]);
                values.push(poollistId[i]);
            } else {
                values.push(Virtualpool);
                values.push(0);
            }

            log.info(values);

            connection.query({sql: sql, values: values}, function (err, rows) {
                if (err) {
                    log.err(err);
                }

            })

        }
        //callback();
        connection.release();

    });
}

//获得奖池最新记录
exports.getScore_pools = function getScore_pools(callback) {
    var sql = "SELECT * FROM score_pool";
    pool.getConnection(function (err, connection) {

        connection.query({sql: sql}, function (err, rows) {
            if (err) {
                log.err(err);
                callback(0);
            } else {
                if (rows.length == 0) {
                    callback(0);
                } else {
                    callback(rows);
                }
            }
        })
        connection.release();

    });
}


//===========================================================================================================================================================
//获得拉霸水位和库存 奖池最新记录
exports.getGamblingGame = function getGamblingGame(callback) {

    var sql = "SELECT * FROM gambling_game_list WHERE nGameID = ?";
    var values = [];
    values.push(gameConfig.gameId);
    pool.getConnection(function (err, connection) {

        connection.query({sql: sql, values: values}, function (err, rows) {
            if (err) {
                log.err(err);
                callback(0);
            } else {
                if (rows.length == 0) {
                    callback(0);
                } else {
                    callback(rows);
                }
            }
        })
        connection.release();

    });
}

//保存库存奖池
exports.Update_GamblingBalanceGold = function Update_GamblingBalanceGold(nGamblingBalanceGold, nGamblingWinPool, callback) {
    var sql = "UPDATE gambling_game_list SET nGamblingBalanceGold=?,nGamblingWinPool=?  WHERE nGameID = ?";

    pool.getConnection(function (err, connection) {
        var values = [nGamblingBalanceGold, nGamblingWinPool, gameConfig.gameId];
        log.info("库存 奖池 id");
        log.info(values);
        connection.query({sql: sql, values: values}, function (err, rows) {
            if (err) {
                log.err(err);
            }

        });

        connection.release();

    });
}


//保存游戏记录
exports.saveGameLog = function saveGameLog(Info, callback) {
    var sql = "INSERT INTO lotterylog_1021(result_array) VALUES(?)";
    var values = [];

    log.info("save game log "+ Info);
    pool.getConnection(function (err, connection) {
        values.push(Info);
        connection.query({sql: sql, values: values}, function (err, rows) {
            connection.release();
            if (err) {
                log.info("saveGameLog");
                log.err(err);
                callback(0);
            } else {
                callback(1, rows.insertId);
            }
            values = [];
        });
    });

};

//查询游戏记录
exports.selectGameLog = function selectGameLog(callback) {

    var sql = "SELECT * FROM lotterylog_1021 order by id desc limit 1";
    pool.getConnection(function (err, connection) {

        connection.query({sql: sql}, function (err, rows) {
            if (err) {
                log.err(err);
                callback(0);
            } else {
                if (rows.length == 0) {
                    callback(0);
                } else {
                    callback(rows);
                }
            }
        });
        connection.release();

    });
};






